package com.example.ayniu.treadmill.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.ayniu.treadmill.MainActivity;
import com.example.ayniu.treadmill.MemberActivity;
import com.example.ayniu.treadmill.MyAddressActivity;
import com.example.ayniu.treadmill.MyBooksActivity;
import com.example.ayniu.treadmill.R;
import com.example.ayniu.treadmill.SetPhoneActivity;
import com.example.ayniu.treadmill.bluetooth.DeviceListActivity;
import com.example.ayniu.treadmill.util.UserInfoUtil;
import com.example.ayniu.treadmill.wxapi.WXEntryActivity;


public class Fragment_set extends Fragment implements View.OnClickListener{

	private Button bt_signout,bt_set_phone,bt_set_bluetooth,bt_set_member;
	private Button bt_set_books,bt_set_address;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// ##layout布局文件转化为view

		//resource 需要加载的布局文件
		//root 加载Layout的布局文件的父ViewGroup
		//布尔值变量 :false 不返回父ViewGroup
		View view = inflater.inflate(R.layout.layout_fragment_set, container, false);

		initView(view);

		return view;
	}

	public void initView(View view){
		bt_signout = (Button) view.findViewById(R.id.bt_signout);
		bt_set_phone = (Button) view.findViewById(R.id.bt_set_phone);
		bt_set_bluetooth = (Button) view.findViewById(R.id.bt_set_bluetooth);
		bt_set_member = (Button) view.findViewById(R.id.bt_set_member);
		bt_set_books = (Button) view.findViewById(R.id.bt_set_books);
		bt_set_address = (Button) view.findViewById(R.id.bt_set_address);
		bt_signout.setOnClickListener(this);
		bt_set_phone.setOnClickListener(this);
		bt_set_bluetooth.setOnClickListener(this);
		bt_set_member.setOnClickListener(this);
		bt_set_books.setOnClickListener(this);
		bt_set_address.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.bt_signout:
				showAlert("您确定要退出账号？",listener_signout);
				break;
			case R.id.bt_set_phone:
				showAlert("您确定要注销绑定手机号？",listener_phone);
				break;
			case R.id.bt_set_bluetooth:
				showAlert("您确定要取消蓝牙配对？",listener_bluetooth);
				break;
			case R.id.bt_set_member:
				Intent intent = new Intent(getActivity(), MemberActivity.class);
				startActivity(intent);
				break;
			case R.id.bt_set_books:
				Intent intent_books = new Intent(getActivity(), MyBooksActivity.class);
				startActivity(intent_books);
				break;
			case R.id.bt_set_address:
				Intent intent_adress = new Intent(getActivity(), MyAddressActivity.class);
				startActivity(intent_adress);
				break;
			default:
				break;
		}
	}

	private void showAlert(String str, DialogInterface.OnClickListener Listener){
		// 创建退出对话框
		AlertDialog isExit = new AlertDialog.Builder(getActivity()).create();
		// 设置对话框标题
		isExit.setTitle("提示");
		// 设置对话框消息
		isExit.setMessage(str);
		// 添加选择按钮并注册监听
		isExit.setButton("确定", Listener);
		isExit.setButton2("取消", Listener);
		// 显示对话框
		isExit.show();
	}
	/**监听对话框里面的button点击事件*/
	DialogInterface.OnClickListener listener_phone = new DialogInterface.OnClickListener()
	{
		public void onClick(DialogInterface dialog, int which)
		{
			switch (which)
			{
				case AlertDialog.BUTTON_POSITIVE:
					//清空本地缓存的USER_PHONE
					UserInfoUtil.setLocalPara("USER_PHONE","");
					//断开蓝牙
					if (getActivity() instanceof MainActivity) {
						MainActivity fca = (MainActivity) getActivity();
						// Stop the Bluetooth chat services
						if (MainActivity.mChatService != null)
							MainActivity.mChatService.stop();
					}
					Intent intent = new Intent(getActivity(), SetPhoneActivity.class);
					startActivity(intent);
					getActivity().finish();
					break;
				case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
					break;
				default:
					break;
			}
		}
	};
	protected DialogInterface.OnClickListener listener_bluetooth = new DialogInterface.OnClickListener()
	{
		public void onClick(DialogInterface dialog, int which)
		{
			switch (which)
			{
				case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
					// Stop the Bluetooth chat services
					if (getActivity() instanceof MainActivity) {
						MainActivity fca = (MainActivity) getActivity();
						fca.layout_bluetooth.setVisibility(View.VISIBLE);
						fca.mViewpager.setVisibility(View.GONE);
						// Stop the Bluetooth chat services
						if (MainActivity.mChatService != null) MainActivity.mChatService.stop();
					}
					Intent intent = new Intent(getActivity(), DeviceListActivity.class);
					startActivity(intent);
					break;
				case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
					break;
				default:
					break;
			}
		}
	};
	DialogInterface.OnClickListener listener_signout = new DialogInterface.OnClickListener()
	{
		public void onClick(DialogInterface dialog, int which)
		{
			switch (which)
			{
				case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
					//清空本地缓存的USER_OPENID
					UserInfoUtil.setLocalPara("USER_OPENID","");
					//清空本地缓存的USER_PHONE
					UserInfoUtil.setLocalPara("USER_PHONE","");
					Intent intent = new Intent(getActivity(), WXEntryActivity.class);
					startActivity(intent);
					getActivity().finish();
					break;
				case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
					break;
				default:
					break;
			}
		}
	};
}
